use std::vec;

fn subseq(s: &mut Vec<char>, idx: usize) {
    if idx == s.len() {
        let mut r = String::from_iter(s.iter());
        r.retain(|c| c != ' ');
        println!("{:?}", r);
        return;
    }

    let c: char = s[idx];
    subseq(s, idx + 1);
    s[idx] = ' ';
    subseq(s, idx + 1);
    s[idx] = c;
}

fn print_all_subseq(s: &str) {
    let mut s: Vec<char> = s.chars().collect();
    subseq(&mut s, 0)
}

fn b_player(cards: &[i32], a: &mut Vec<i32>, b: &mut Vec<i32>, level: i32) -> (Vec<i32>, Vec<i32>) {
    println!("[{}][B] cards: {:?}", level, cards);
    if cards.len() == 1 {
        b.push(cards[0]);
        let res = (a.clone(), b.clone());
        b.pop();
        return res;
    }

    let end = cards.len() - 1;
    print!("[{}] B抽第一张:", level);
    b.push(cards[0]);
    println!("{:?}", b);
    let (ac1, bc1) = a_player(&cards[1..], a, b, level + 1);
    println!("[{}] b: ac1: {:?}, bc1: {:?}\n", level, ac1, bc1);
    b.pop();
    println!("[{}] cards: {:?}", level, cards);
    print!("[{}] b抽最后一张：", level);
    b.push(cards[end]);
    println!("[{}] {:?}", level, b);
    let (ac2, bc2) = a_player(&cards[..end], a, b, level + 1);
    println!("[{}] b: ac2: {:?}, bc2: {:?}\n", level, ac2, bc2);
    b.pop();

    let s1 = bc1.iter().sum::<i32>();
    let s2 = bc2.iter().sum::<i32>();

    if s1 > s2 {
        (ac1, bc1)
    } else {
        (ac2, bc2)
    }
}

fn a_player(cards: &[i32], a: &mut Vec<i32>, b: &mut Vec<i32>, level: i32) -> (Vec<i32>, Vec<i32>) {
    println!("[A-{}] cards: {:?}", level, cards);
    if cards.len() == 1 {
        a.push(cards[0]);
        let res = (a.clone(), b.clone());
        a.pop();
        return res;
    }

    let end = cards.len() - 1;
    print!("[A-{}] A抽第一张：", level);
    a.push(cards[0]);
    println!("[A] {:?}", a);
    let (ac1, bc1) = b_player(&cards[1..], a, b, level + 1);
    println!("a>>> ac1: {:?}, bc1: {:?}\n", ac1, bc1);
    a.pop();
    println!("cards: {:?}", cards);
    print!("[A-{}] A抽最后一张:", level);
    a.push(cards[end]);
    println!("[{}] [A] {:?}", level, a);
    let (ac2, bc2) = b_player(&cards[..end], a, b, level + 1);
    println!("[{}] a>>> ac2: {:?}, bc2: {:?}\n", level, ac2, bc2);
    a.pop();

    let s1 = ac1.iter().sum::<i32>();
    let s2 = ac2.iter().sum::<i32>();

    if s1 > s2 {
        (ac1, bc1)
    } else {
        (ac2, bc2)
    }
}

fn second(cards: &[i32]) -> i32 {
    if cards.len() == 1 {
        return 0;
    }
    let end = cards.len() - 1;

    std::cmp::min(frist(&cards[1..]), frist(&cards[..end]))
}

fn frist(cards: &[i32]) -> i32 {
    if cards.len() == 1 {
        return cards[0];
    }
    let end = cards.len() - 1;

    std::cmp::max(
        cards[0] + second(&cards[1..]),
        cards[end] + second(&cards[..end]),
    )
}

fn play_card(cards: &[i32]) {
    let mut a: Vec<i32> = vec![];
    let mut b: Vec<i32> = vec![];

    let (ac1, bc1) = a_player(cards, &mut a, &mut b, 1);
    println!(
        "A({}):{:?}, B({}): {:?}",
        ac1.iter().sum::<i32>(),
        ac1,
        bc1.iter().sum::<i32>(),
        bc1
    );
    println!("score: f: {}, s:{}", frist(cards), second(cards));
}

fn trans(s: &[char], v: &mut Vec<char>) {
    if s.len() == 0 {
        println!("{:?}", String::from_iter(v.iter()));
        return;
    }

    match s[0] {
        c @ '3'..='9' => {
            let x = (c.to_digit(10).unwrap() as u8 + 'a' as u8 - 1) as char;
            v.push(x);
            trans(&s[1..], v);
            v.pop();
        }
        '0' => {
            if s.len() > 1 {
                let x = s[0].to_string() + &s[1].to_string();
                let x = (x.parse::<u8>().ok().unwrap() + 'a' as u8 - 1) as char;
                v.push(x);
                trans(&s[2..], v);
                v.pop();
            } else {
                v.push('-');
                trans(&s[1..], v);
                v.pop();
            }
        }
        '1' => {
            v.push('a');
            trans(&s[1..], v);
            v.pop();
            if s.len() > 1 {
                let x = s[0].to_string() + &s[1].to_string();
                let x = (x.parse::<u8>().ok().unwrap() + 'a' as u8 - 1) as char;
                v.push(x);
                trans(&s[2..], v);
                v.pop();
            }
        }
        '2' => {
            v.push('b');
            trans(&s[1..], v);
            v.pop();
            if s.len() > 1 {
                if '0' <= s[1] && s[1] <= '6' {
                    let x = s[0].to_string() + &s[1].to_string();
                    let x = (x.parse::<u8>().ok().unwrap() + 'a' as u8 - 1) as char;
                    v.push(x);
                    trans(&s[2..], v);
                    v.pop();
                }
            }
        }
        _ => {
            println!("undefined")
        }
    }
}

fn trans_methods(s: &str) {
    let s: Vec<char> = s.chars().collect();
    let mut vchar: Vec<char> = vec![];
    trans(&s, &mut vchar);
}

pub fn recursion_main() {
    print_all_subseq("1234");
    play_card(&[5, 2, 45, 69, 80, 100, 4, 7]);

    trans_methods("11112759832501421");
}
